package na.learn.java.corepatterns.hashing.division;

import com.sun.org.apache.xalan.internal.xsltc.compiler.Pattern;

public class MyType {

	private String name;

	private int age;

	public MyType(String name, int age) {		
		this.name = name;
		this.age = age;
	}

	public boolean equals(Object obj) {
		if (obj == null || !(obj instanceof MyType) || !(obj.getClass().equals(this.getClass())))
			return false;
		
		if (obj == this)
			return true;
		
		MyType myObj = (MyType) obj;
		if (this.name.equals(myObj.getName()) && this.age == myObj.getAge())
			return true;
		else
			return false;

	}

	public int hashCode() {

		boolean flag = false;
		int hashValue, counter = hashValue = 0;

		char[] strArr = name.trim().toCharArray();
		for (char ch : strArr){
			//if(flag == true)
			hashValue ^= (ch >> hashValue);
			/*else
				hashValue ^= (ch << hashValue);*/
			flag = !flag;
		}
		//System.out.println("name: " + name + ", hashCode: " + na.learn.java.utils.Pattern.getBitPattern(hashValue));

		return hashValue + age;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public String toString() {
		return na.learn.java.utils.Pattern.toUnsignedString(hashCode(),1);	
	}

	public static void main(String[] args) {

		char ch = 'A';
		ch = (char) (ch + 1);
		System.out.println("" + ch);
	

	MyType myObj1 = new MyType("ABC", 11);

	MyType myObj2 = new MyType("BCD", 22);
	 MyType myObj3 = new MyType("CDE", 33);
	 MyType myObj4 = new MyType("DEF", 44);
	 MyType myObj5 = new MyType("EFG", 55);
	 
	 MyType myObj1_1 = new MyType("ABC", 12);
	 MyType myObj1_2 = new MyType("BCD", 12);
	 
	 
	 
	 myObj1.hashCode();
	// myObj1_1.hashCode();
	 //myObj1_2.hashCode();
	 myObj2.hashCode();
	 myObj3.hashCode();
	 myObj4.hashCode();
	 myObj5.hashCode(); 
	 

	 }
}
